package com.shk.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.mysql.jdbc.StringUtils;
import com.shk.dao.MemberDao;
import com.shk.dao.OrderDao;
import com.shk.service.ReportService;
import com.shk.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public List<Map> findNameAndCount() {
        List<Map> list = orderDao.findNameAndCount();
        return list;
    }

    @Override
    public Map getBusinessReportData() throws Exception {
        /*  reportDate:null,
                    todayNewMember :0,  /*新增会员数*//*
        totalMember :0,         *//*总会员数*//*
                thisWeekNewMember :0,  *//*本周新增会员数*//*
                thisMonthNewMember :0,*//*本月新增会员数*//*
                todayOrderNumber :0,    *//*今日预约数*//*
                todayVisitsNumber :0,   *//*今日到诊数*//*
                thisWeekOrderNumber :0, *//*本周预约数*//*
                thisWeekVisitsNumber :0,    *//*本周到诊数*//*
                thisMonthOrderNumber :0,*//*本月预约数*//*
                thisMonthVisitsNumber :0,*//*本月到诊数*/
         /*获取当前日期*/
        Date date = DateUtils.getToday();
        String today = DateUtils.parseDate2String(date);
        /*获取本周一的日期*/
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        /*获取本月第一天的日期*/
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        /*获取当天新增会员数*/
        Integer todayNewMember = memberDao.findCountByToday(today);
        /*获取总的会员数*/
        Integer totalMember = memberDao.findCount();
        /*获取本周新增会员数  查询大于等于周一的数据*/
      Integer thisWeekNewMember = memberDao.findCountByThisWeek(thisWeekMonday);
      /*获取本月新增会员数*/
      Integer thisMonthNewMember = memberDao.findCountByThisMonth(firstDay4ThisMonth);
      /*获取今日预约数*/
       Integer todayOrderNumber = orderDao.findByToday(today);
       /*获取今日到诊数*/
        Integer todayVisitsNumber = orderDao.findOrderStatusCountByToday(today);
        /*获取本周预约数*/
        Integer thisWeekOrderNumber = orderDao.findCountByThisWeek(thisWeekMonday);
        /*获取本周到诊数*/
        Integer thisWeekVisitsNumber = orderDao.findOrderStatusCountByThisWeek(thisWeekMonday);
        /*获取本月预约数*/
        Integer thisMonthOrderNumber = orderDao.findCountByThisMonth(firstDay4ThisMonth);
        /*获取本月到诊数*/
        Integer thisMonthVisitsNumber = orderDao.findOrderStatusCountByThisMonth(firstDay4ThisMonth);
        /*获取热门套餐数据*/
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map map = new HashMap();

        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",hotSetmeal);
        return map;
    }

    @Override
    public Map<String, Integer> getMemberReportByDate(String start, String end)throws Exception {
        List<Map<String, Object>> member = memberDao.findMemberReportByDate(start+"-01",end+"-31");
        //将字符串的日期转换成date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date dateStart = sdf.parse(start);
        Date dateEnd = sdf.parse(end);
        //将date类型转换文日历类进行操作，获取当前时间
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        //日历类可以指定时间，指定为用户输入的时间
        bef.setTime(dateStart);
        aft.setTime(dateEnd);
        //计算用户输入时间，月份的差值
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        int total = result+month;
        //将数据里的数据 全部进行遍历，在指定的时间段内没有数据就自动填充为0，最后返回一个map，即可。
        Map<String,Integer> memberDate = new HashMap<>();
        for (int i = 0; i <= total; i++) {
            //从指定时间开始 ，每次循环将月份 +1  ,对数据库查询数据遍历，没有则自动填充
            //因为外国时间，所以月份+1
            int i1 = bef.get(Calendar.MONTH)+1+i;   //2020-1
            int i2 = bef.get(Calendar.YEAR);
            if ( i1 > 12){
                //防止月数过大，出现超过12 情况
                if (i1%12 == 0){
                    i2 = i2+i1/12-1;
                    i1 = 12;
                }else{
                    i2 = i2 + i1/12;
                    i1 = i1%12;
                }
            }
            String befMonth = "";
            if (i1<10){
                befMonth = "0"+i1;
            }else {
                befMonth = i1+"";
            }

            String dateYear = i2+"-"+befMonth;  //2020-01
            for (Map<String, Object> map : member) {
                String regTime = (String)map.get("regTime");
                if (dateYear.equals(regTime)){
                    Long count = (Long) map.get("count");
                    // int i2 = Integer.parseInt(count);
                    memberDate.put(dateYear,Integer.parseInt(count+"") );
                    //如果有相等的了，就不用进行后续比较，因为map key唯一
                    break;
                }else {
                    memberDate.put(dateYear,0);
                }
            }
        }
        return memberDate;
    }
}
